package com.kizuki.bo;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

/**
 * 执行顺序
 * 构造方法 -> @PostConstruct -> InitializingBean.afterPropertiesSet -> init-method(xml或者 @Bean(initMethod = "initMethod"))
 */
@Slf4j
@Component
public class ConsturctBean implements InitializingBean {

    @Value("${schedule.enable}")
    private String initStr = "";

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("== ConsturctBean InitializingBean ==, initStr:{}", initStr);
    }

    // return void and execute only once
    @PostConstruct
    public void init(){
        log.info("== ConsturctBean @PostConstruct ==, initStr:{}", initStr);
    }

    public void initBean(){
        log.info("== ConsturctBean initMethod");
    }

    @PreDestroy
    public void preDestroy(){
        log.info("== ConsturctBean PreDestroy");
    }

    @PreDestroy
    public void destroy(){
        log.info("== ConsturctBean PreDestroy");
    }


    // or xml
    @Bean(initMethod = "initBean", destroyMethod = "destroy")
    public ConsturctBean getBean(){
        return new ConsturctBean();
    }


}
